package cn.gok;

import java.io.*;
import java.util.Comparator;
import java.util.TreeSet;

/*1 创建类 School类：name,address,  birthYear成立的年份：int （2002；2010）
        实现序列化接口，void writeToFile();//采用对象流写入将两个school对象写入到 1.dat中，
        void readFromFile();//从1.dat中读取数据到对象，并显示出来



        2 创建类 Student：  int sid(学号) String name(姓名) ,int age;
        要求使用两种方法实现自定义比较规则： 先按学号排，如果学号学号重复，按年龄排。
        并遍历输出结果*/

class School implements Serializable{
    private String name;
    private String address;
    private int birthYear;

    public School(String name, String address, int birthYear) {
        this.name = name;
        this.address = address;
        this.birthYear = birthYear;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getBirthYear() {
        return birthYear;
    }

    public void setBirthYear(int birthYear) {
        this.birthYear = birthYear;
    }

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", birthYear=" + birthYear +
                '}';
    }
    public void writeToFile(School school1,School school2) throws IOException {
        ObjectOutputStream obs=new ObjectOutputStream(new FileOutputStream("day5/1.dat"));
        obs.writeObject(school1);
        obs.writeObject(school2);
        obs.close();
        System.out.println("写入成功");
    }

    public void readFromFile() throws IOException, ClassNotFoundException {
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("day5/1.dat"));
        try {
            while (true){
                Object obj = ois.readObject();
                System.out.println(obj.toString());
            }
        } catch (EOFException e) { //通过捕获文件末尾异常，跳出死循环
            System.out.println("文件读取结束");
        }
        ois.close();
    }
}

//方法1，让自定义类实现可比较接口
class Student implements Comparable<Student> {
    private int sid;
    private String name;
    private int age;

    public Student(int sid, String name, int age) {
        this.sid = sid;
        this.name = name;
        this.age = age;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        if(this.sid<o.sid){
            return -1;
        }else if(this.sid==o.sid){
            return this.age-o.age;
        }
        return 1;
    }
}

//2、自定义比较器实现按名字的长度排序
class MySrtudentCompartor implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        if(o1.getSid()<o2.getSid()){
            return -1;
        }else if(o1.getSid()==o2.getSid()){
            return o1.getAge()-o2.getAge();
        }
        return 1;
    }
}
public class Homework {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        School school1=new School("清华","北京",2002);
        School school2=new School("北大","北京",2010);
        school1.writeToFile(school1,school2);
        school1.readFromFile();

        TreeSet<Student> set1=new TreeSet<>();
        set1.add(new Student(1,"小虎",19));
        set1.add(new Student(2,"小洛",20));
        set1.add(new Student(3,"小王",20));
        set1.add(new Student(2,"小易",19));
        set1.stream().forEach(System.out::println);
        System.out.println("---------------------");
        TreeSet<Student> set2=new TreeSet<>(new MySrtudentCompartor());
        set2.add(new Student(1,"小旭",21));
        set2.add(new Student(2,"小其",21));
        set2.add(new Student(1,"小陈",20));
        set2.add(new Student(3,"小徐",21));
        set2.stream().forEach(System.out::println);
    }
}